package com.snail.billing.session;

import java.io.StringReader;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.NameValuePair;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.snail.billing.DataCache;
import com.snail.billing.net.BillingEncode;
import com.snail.billing.net.BillingRegisterArrayHttpSession;
import com.snail.billing.net.BillingSecurity;
import com.snail.billing.util.BillingUtil;

public class RegisterCommonSession extends BillingRegisterArrayHttpSession {

	public RegisterCommonSession(String account, String pwd) {
		DataCache cache = DataCache.getInstance();
		String host = cache.importParams.hostBusiness;
		String address = String.format("http://%s/ws/register.do?method=sdoRegisterPassport", host);
		setAddress(address);
		addBillingPair("userName", BillingEncode.enCode(account, "GBK"));
		addBillingPair("pwd", pwd);
		addBillingPair("email", "woniu@woniu.woniu");
		addBillingPair("gid", cache.importParams.gameId);
		addBillingPair("rid", "0");
		addBillingPair("sid", "0");
		addBillingPair("ip", BillingUtil.getLocalIp());
		addBillingPair("pid", "7");
		addBillingPair("spreaderId", "0");
		addBillingPair("extParams", buildExtra());
		buildParamPair();
	}

	private String buildExtra() {
		DataCache cache = DataCache.getInstance();
		String channelId = "SJ";
		if (cache.importParams.extra != null) {
			channelId = cache.importParams.extra.getString("channelId");
		}
		String appVersion = BillingUtil.getAppVersion();
		String deviceId = BillingUtil.getIMEICode();
		String provider = BillingUtil.getProviderName();
		//
		StringBuilder builder = new StringBuilder();
		builder.append("JSON:{\"regFromTypeValue\":\"");
		builder.append(channelId);
		builder.append("|");
		builder.append(appVersion);
		builder.append("|");
		builder.append(deviceId);
		builder.append("|");
		builder.append(provider);
		builder.append("\"}");
		return builder.toString();
	}

	@Override
	public void buildParamPair() {
		BillingSecurity security = getSecurity();
		addParamPair("accessId", security.accessId);
		addParamPair("accessPasswd", security.accessPasswd);
		addParamPair("accessType", security.accessType);
		for (NameValuePair pair : getBillingPairList()) {
			addParamPair(pair.getName(), pair.getValue());
		}
		addParamPair("sVerifyStr", buildVerifyStr(security, getBillingPairList()));
	}

	protected String buildVerifyStr(final BillingSecurity security, final List<NameValuePair> list) {
		StringBuilder builder = new StringBuilder();
		builder.append(security.accessId);
		builder.append(security.accessPasswd);
		builder.append(security.accessType);
		for (NameValuePair pair : list) {
			builder.append(pair.getValue());
		}
		builder.append(security.seed);
		String origin = builder.toString();
		return BillingEncode.MD5(origin);
	}

	@Override
	public BillingSecurity getSecurity() {
		if (DataCache.getInstance().isSandbox) {
			return BillingSecurity.securitySandbox2;
		}
		return BillingSecurity.security;
	}

	public static class XmlData {

		private String code;

		private String message;

		public XmlData(String xmlString) {
			try {
				SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
				RegisterCommonHander handler = new RegisterCommonHander();
				saxParser.parse(new InputSource(new StringReader(xmlString)), handler);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		public String getCode() {
			return code;
		}

		public String getMessage() {
			return message;
		}

		private class RegisterCommonHander extends DefaultHandler {

			@Override
			public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
				if (localName.equals("RESULT")) {
					for (int i = 0; i < attributes.getLength(); i++) {
						String name = attributes.getLocalName(i);
						String value = attributes.getValue(i);
						if (name.equals("msgcode")) {
							code = value;
						} else if (name.equals("message")) {
							message = value;
						}
					}
				}
			}

		}
	}

}
